package com.mima.minaserver.client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.mima.minaserver.server.ByteArrayCodecFactory;


public class IotServerClient extends IoHandlerAdapter {
	

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {		
		List<String> uuids = getUUIDList();
		for (int i = 0; i < 500; i++) {
			MyThread thread = new MyThread(uuids.get(i));
			thread.start();
		}

	}
	
	public static byte[] hexStringtoBytes(String str) {
		if (str == null || str.trim().equals("")) {
			return new byte[0];
		}

		byte[] bytes = new byte[str.length() / 2];
		for (int i = 0; i < str.length() / 2; i++) {
			String subStr = str.substring(i * 2, i * 2 + 2);
			bytes[i] = (byte) Integer.parseInt(subStr, 16);
		}

		return bytes;
	}
	public static List<String> getUUIDList() throws IOException{
		
		Resource resource = new ClassPathResource("uuid.txt");
		File file = resource.getFile();
		BufferedReader br = new BufferedReader(new FileReader(file));
		String uuids = br.readLine();
		List<String> list = Arrays.asList(uuids.split(","));
		br.close();
		return list;
		
	}

}
 class MyThread extends Thread{
	
	String id;
	public MyThread(String id){
		this.id = id;
	}
	@Override
	public void run() {
		IoConnector connector = new NioDatagramConnector();
		connector = new NioDatagramConnector();
		connector.setHandler(new IotServerClient());
		connector.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new ByteArrayCodecFactory()));
		ConnectFuture connFuture = connector.connect(new InetSocketAddress(
				"47.92.28.251", 8989));
		// ConnectFuture connFuture = connector.connect(new
		// InetSocketAddress(InetAddress.getLocalHost().getHostAddress(),
		// 8989));
		connFuture.awaitUninterruptibly();
		IoSession session = connFuture.getSession();
		byte[] heartp = "HEARTP".getBytes();
		//byte[] uuid = "2b4140dbc66f4ec7bfe4a42e82aa57cc".getBytes();
		byte[] uuid = id.getBytes();
		byte[] dateTime = IotServerClient.hexStringtoBytes("5ad605d4");
		byte[] userpasswd = "test1234,test1234".getBytes();
		byte[] returnBytes = new byte[heartp.length+uuid.length+dateTime.length+dateTime.length+userpasswd.length];
		System.arraycopy(heartp, 0, returnBytes, 0, 6);
		System.arraycopy(uuid, 0, returnBytes, 6, 32);
		System.arraycopy(dateTime, 0, returnBytes, 38, dateTime.length);
		System.arraycopy(userpasswd, 0, returnBytes, 46, userpasswd.length);
		while(true){
			session.write(IoBuffer.wrap(returnBytes));
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
	}
	 
 }
